package co.com.smart.eagle.server.filter;

import java.io.IOException;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet Filter implementation class GWTCacheControlFilter
 */
public class GWTCacheControlFilter implements Filter {

    /**
     * Default constructor. 
     */
    public GWTCacheControlFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		 HttpServletRequest httpRequest = (HttpServletRequest) request;
         String requestURI = httpRequest.getRequestURI();

         //No hace cach� JavaScript
         if (requestURI.contains("nocache") || requestURI.contains("SmartEagle.html")) {
  
                Date now = new Date();
                HttpServletResponse httpResponse = (HttpServletResponse) response;
                httpResponse.setDateHeader("Date", now.getTime());
                // one day old
                httpResponse.setDateHeader("Expires", now.getTime() - 86400000L);
                httpResponse.setHeader("Pragma", "no-cache");
                httpResponse.setHeader("Cache-control", "no-cache, no-store, must-revalidate");
         }       
		
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
